#!/usr/bin/perl

require Burpsuite::Parser;
require Frontier::Client;

use File::Basename;
use YAML;

# step 1: open file
open my $settingsfh, '<', 'options.yaml' 
  or die "can't open config file: $!";

# step 2: convert YAML file to perl hash ref
my $config = YAML::LoadFile($settingsfh);

my $bpx = new Burpsuite::Parser;

my $filepath = $ARGV[0];
my ($filename, $directories, $suffix) = fileparse($filepath);

my $parser = $bpx->parse_file( $filepath );
   #a Burpsuite::Parser Object

my @results = $parser->get_all_issues();
   #an Array of Burpsuite::Parser::Issue Objects

##my @priorities = {"High", "Medium
 
foreach my $h ( @results ) {
##   print "Type: " . $h->type . "\n";
##   print "Serial: " . $h->serial_number . "\n";
##   print "Severity: " . $h->severity . "\n";
##   print "Host: " . $h->host . "\n";
   print "Name: " . $h->name . "\n";
##   print "Location: " . $h->location . "\n";
##   print "Path: " . $h->path . "\n";
##   print "Issue Background: " . $h->issue_background . "\n";
##   print "Remediation Background: " . $h->remediation_background . "\n";
##   print "Issue Detail: " . $h->issue_detail . "\n";

## if we want to gen a ticket (priority

   ### do the translation
   my $summary =  $h->name;
   my $description = $h -> issue_background;
   my $affects = $h->host . $h->location;
   my $priority = $h->severity;
   my $proof = $h->issue_detail; 
   my $recommendations = $h->remediation_background;
   my $references = "Burp Suite Report: " . $filename;

   my %attributes; 

   ## Stuff the attributes in a hash
   $attributes{'type'} = "defect";
   $attributes{'priority'} = $priority;
   $attributes{'proof'} = $proof;
   $attributes{'recommendations'} = $recommendations;
   $attributes{'references'} = $references;


   ### Getting stupid error when i try this: 
   $attributes{'damagepotential'} = '0';
   $attributes{'reproducibility'} = '0';
   $attributes{'exploitability'} = '0';
   $attributes{'affectedusers'} = '0';
   $attributes{'discoverability'} = '0';




##  maybe something like this (ruby+php)
## 
##  print $yaml['settings']['server']


   ## Set up Project
   my $project = $config->{'settings'}->{'project'};
   my $username = $config->{'settings'}->{'username'};
   my $password = $config->{'settings'}->{'password'};
   my $server = $config->{'settings'}->{'server'};

   $url = "https://" . $username . ":" . $password ."\@" . $server . "/projects/". $project ."/login/xmlrpc";

   ## create the bridge to trac
   my $server = Frontier::Client->new( url => $url );
				## For some reason, proxy is ignored...
				##proxy => 'http://127.0.0.1:8080/');

   ## run the ticket creation function...
   $response = $server->call('ticket.create', $summary , $description , {%attributes});

   print "Creating ticket: " . $_ . " - " . $response . "\n";

   ## foreach (@$response) {
   ##   print "Creating ticket: " . $_ . " - " . $summary . "\n";
   ## }


   ##read a line first - so we can debug
   $userinput =  <STDIN>;
}
